package com.hzsai.nettylearn.server;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * hzsai 2019/11/14
 * usage: 同步IO server启动器
 *
 * PostConstruct,
 * implements initializing bean,
 * BeanPostProcess - PostProcessBeforeInitialization, PostProcessAfterInitialization
 *
 * 执行顺序：
 * 1. PostProcessBeforeInitialization
 * 2. InitializingBean的afterPropertiesSet
 * 3. init-method
 * 4. PostProcessAfterInitialization
 *
 * @PostConstruct: 在自动注入完成之后执行
 * @PreDestory 在Bean被销毁前执行
 **/
//@Component
public class TimeBlockingSocketTaskStarter {

//    TimeServer server;
//
//    TimeClient client;

    @Autowired
    TimeServer server;

    @Autowired
    TimeClient client;

//    @Autowired
//    public void setServer(TimeServer server) {
//        this.server = server;
//    }
//
//    @Autowired
//    public void setClient(TimeClient client) {
//        this.client = client;
//    }

    public TimeBlockingSocketTaskStarter() throws IOException, InterruptedException {
//        Runnable r = () -> {
//            try {
//                server.start();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        };
//        Thread t = new Thread(r);
//
//        t.start();
//        // server.start();
//        client.start();
    }

    /**
     * 需要确保server, client已经创建了才能执行
     * 所以这里使用 @PostConstruct注解
     * @throws IOException
     * @throws InterruptedException
     */
    @PostConstruct
    public void postConstruct() throws IOException, InterruptedException {
//        Runnable r = () -> {
//            try {
//                server.start();
//            } catch (IOException e) {
//                e.printStackTrace();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        };
//        Thread t = new Thread(r);
//
//        t.start();
//        // server.start();
//        client.start();
    }
}
